<template>
  <div class="desc-item">
    <div class="form-item">后评估</div>
    <el-form :model="reviewData" label-width="120px">
      <div v-for="category in reviewData" :key="category.value">
        <div class="table-title">{{ category.review_category }}</div>
        <el-form-item label="是否有问题">
          <el-radio-group v-model="category.review_question" :disabled="!isShowApprovalOperation('first') || isSubmitted">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" v-if="category.review_question">
          <el-input 
            v-model="category.review_cause" 
            type="textarea" 
            :rows="2" 
            :disabled="!isShowApprovalOperation('first') || isSubmitted"
          />
        </el-form-item>
        <el-form-item label="解决措施" v-if="category.review_question">
          <el-input 
            v-model="category.review_solution_measure" 
            type="textarea" 
            :rows="2" 
            :disabled="!isShowApprovalOperation('first') || isSubmitted"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input 
            v-model="category.review_comment" 
            type="textarea" 
            :rows="2" 
            :disabled="!isShowApprovalOperation('first') || isSubmitted"
          />
        </el-form-item>
      </div>
    </el-form>
    <div v-if="projectFiles[processStageName].length > 0">
      <div class="table-title">上传附件</div>
      <FilesUpload
        :fileTypes="projectFiles[processStageName]"
        :projectInfo="projectInfo"
        :readOnly="!isShowApprovalOperation('first')"
        @getProjectFilesInfo="$emit('getProjectFilesInfo')"
      />
    </div>
    <div class="mb-16 flex" v-if="isShowApprovalOperation('first') && !isSubmitted">
        <el-button class="ml-auto" type="primary" size="default"
          @click="approvalProcess('agree', 'projectManager')">同意</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import * as IctApi from '@/api/project/ict'
import projectReviewInfoDesc from '@/views/project/projectDetails/mixins/projectReviewDetailsMixin'
import FilesUpload from '@/views/project/projectDetails/components//FilesUpload.vue';

// emit事件合集
const emit = defineEmits(['getApprovalProcess', 'getProjectInfo', 'getProjectFilesInfo']);

// Props传值
const props = defineProps({
  isShowApprovalOperation: Function,
  projectInfo: Object,
  projectReviewDetails: Object,
  processStageName: String,
  taskName: String,
  role: String,
  processList: Array,
  projectFiles: Object
})
const { isShowApprovalOperation, projectInfo, projectReviewDetails, processStageName, taskName, role, processList } = toRefs(props);


const reviewData = ref(projectReviewInfoDesc)
if(projectInfo.value.reviewInfo.length > 0) {
  reviewData.value = projectInfo.value.reviewInfo
}
const isSubmitted = ref(false)

const approvalProcess = (result, currentRole) => {
  ElMessageBox.confirm(`是否${result === 'agree' ? '同意' : '驳回'}该流程单？`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    if (result === 'agree') {
      const agreeProcessData = {
        assignedUserIds: [],
        id: processList.value[0].id,
        reason: '同意',
        copyUserIds: [],
        variables: {}
      }
      
      const agreeProjectReviewData = reviewData.value.map(category => ({
        ...category,
        project_code: projectInfo.value.project_code
      }))

      projectInfo.value.finished = true
      
      const agreeData = {
        project_index_do: projectInfo.value,
        task_approve_req_vo: agreeProcessData,
        project_review_list: agreeProjectReviewData,
      }
      await IctApi.agreeProjectReviewProcess(agreeData).then(() => {
        isSubmitted.value = true
        emit('getApprovalProcess')
        emit('getProjectInfo')
      })
    }
    ElMessage({
      type: 'success',
      message: '提交成功'
    })
  })
}

</script>

<style lang="scss" scoped>
@import url('@/views/project/projectDetails/styles/transfer.css');

</style>